package com.evaluationSystem.controller;

import com.evaluationSystem.config.R;
import com.evaluationSystem.entity.Appraise;
import com.evaluationSystem.entity.SysLogOperationEntity;
import com.evaluationSystem.entity.dto.AppraiseDto;
import com.evaluationSystem.entity.dto.Fraction;
import com.evaluationSystem.entity.vo.AnalyseOwnVo;
import com.evaluationSystem.entity.vo.AppraiseDetail;
import com.evaluationSystem.entity.vo.AppraiseVo;
import com.evaluationSystem.service.AppraiseService;
import com.evaluationSystem.util.AssertUtils;
import com.evaluationSystem.util.CommonUtil;
import com.evaluationSystem.util.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价控制器
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/appraise")
public class AppraiseController {
    @Value("${file.appraisePath}")
    private String filePath;

    //评价控制器
    private final AppraiseService appraiseService;

    @RequestMapping(value = "/getEvaluateObject", method = RequestMethod.POST)
    public R<AnalyseOwnVo> getEvaluateObject(@RequestBody Appraise appraise) {
        AnalyseOwnVo evaluateObject = appraiseService.getEvaluateObject(appraise);
        return R.success(evaluateObject);
    }

//    @RequestMapping(value = "/logCx", method = RequestMethod.GET)
//    public R<SysLogOperationEntity> logCx() {
//        SysLogOperationEntity list= appraiseService.seleceSyslog();
//        return R.success(list);
//    }

    @RequestMapping(value = "/getfraction", method = RequestMethod.POST)
    public R<List<Fraction>> getfraction() {
        return R.success(appraiseService.getfraction());
    }

//    @RequestMapping(value = "/getAppraiseList", method = RequestMethod.POST)
//    public R<PageData<Appraise>> qgetAppraiseList(@RequestBody Appraise appraise) {
//        return R.success(appraiseService.getAppraisePage(appraise));
//    }


 @RequestMapping(value = "/getAppraiseList", method = RequestMethod.POST)
    public R<PageData<AppraiseVo>> getAppraiseList(@RequestBody Appraise appraise) {
     System.out.println(appraise);
     appraise.setAppraiseNotice(appraise.getAppraiseNotice());
     System.out.println("是否发短信：0未评估 1已评估':"+appraise.getAppraiseNotice());
     appraise.setAppraiseStatus(appraise.getAppraiseStatus());
     System.out.println("评估状态：0未通知 1已通知'："+appraise.getAppraiseStatus());
        return R.success(appraiseService.getAppraisePage(appraise));
    }

    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public void download(HttpServletResponse response, String fileName) {
        CommonUtil.downTemplates(response, fileName, filePath);
    }

    // 导出关系
    @RequestMapping(value = "/exportAppraise", method = RequestMethod.GET)
    public void exportAppraise(HttpServletResponse response, @RequestParam Map<String, Object> params) throws Exception {
        appraiseService.exportAppraise(response, params);
    }

    @RequestMapping(value = "/uploadAppraise", method = RequestMethod.POST)
    public R<HashMap<String, Object>> uploadAppraise(@RequestPart(value = "file") MultipartFile multipartFile, String questionId) {
        HashMap<String, Object> resultMap = appraiseService.uploadAppraise(multipartFile, questionId);
        return R.success(resultMap);
    }

    @RequestMapping(value = "/insertAppraise", method = RequestMethod.POST)
    public R<String> insertAppraise(@RequestBody AppraiseDto appraiseDto) {
        AssertUtils.isArrayEmpty(appraiseDto.getSubList().toArray(new AppraiseDto.subList[0]), "评估关系");

        appraiseService.insertAppraise(appraiseDto);
        return R.success("新增成功");
    }


    // todo 修改评估关系
    @RequestMapping(value = "/updateAppraise", method = RequestMethod.POST)
    public R<String> updateAppraise(@RequestBody AppraiseDto appraiseDto) {
        AssertUtils.isArrayEmpty(appraiseDto.getSubList().toArray(new AppraiseDto.subList[0]), "评估关系");

        appraiseService.updateAppraise(appraiseDto);
        return R.success("修改成功");
    }

    @RequestMapping(value = "/getAppraiseShow", method = RequestMethod.POST)
    public R<PageData<Appraise>> getAppraiseShow(@RequestBody Appraise appraise) {
        return R.success(appraiseService.getAppraiseShow(appraise));
    }

    // todo 问卷查询
    @RequestMapping(value = "/getAppraiseDetail", method = RequestMethod.GET)
    public R<AppraiseDetail> getAppraiseDetail(@RequestParam String appraiserPhone, @RequestParam String assessedId, @RequestParam String questionId) {
        return R.success(appraiseService.getAppraiseDetail(appraiserPhone, assessedId, questionId));
    }

    @RequestMapping(value = "/deleteAppraise", method = RequestMethod.POST)
    public R<String> deleteAppraise(@RequestBody Map<String, Object> map) {
        List questionIdList = (List) map.get("questionIdList");
        List appraiserIdList = (List) map.get("appraiserIdList");

        AssertUtils.isListEmpty(questionIdList, "questionIdList");
        AssertUtils.isListEmpty(appraiserIdList, "appraiserIdList");

        appraiseService.deleteAppraise(map);
        return R.success("删除成功");
    }

    @RequestMapping(value = "/sendAnswerMessage", method = RequestMethod.POST)
    public R<String> sendAnswerMessage(@RequestBody Map<String, Object> map) {
        String questionId = (String) map.get("questionId");
        List appraiserPhoneList = (List) map.get("appraiserPhoneList");

        AssertUtils.isBlank(questionId, "questionId");
        AssertUtils.isListEmpty(appraiserPhoneList, "appraiserPhoneList");

        return appraiseService.sendAnswerMessage(questionId, appraiserPhoneList);
    }
}
